﻿@{
    Layout = "";
}
@model Nop.Plugin.DiscountRules.HasOneProduct.Models.RequirementModel
@using Nop.Web.Framework;

<script type="text/javascript">
    $(document).ready(function () {
        $('#saveHasOneProductrequirement@(Model.RequirementId)').click(function () {
            var productIds = $("#@Html.FieldIdFor(model => model.Products)").val();
            var discountId = @Model.DiscountId;
            var requirementId = @Model.RequirementId;
                
            var postData = {
                discountId: discountId, 
                discountRequirementId: requirementId,
                productIds: productIds
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache:false,
                type: "POST",
                url: "@(Url.Action("Configure", "DiscountRulesHasOneProduct"))",
                data: postData,
                success: function (data) {
                    $('#pnl-save-requirement-result@(Model.RequirementId)').fadeIn("slow").delay(1000).fadeOut("slow");
                    //notify parent if it's a new requirement
                    @if (Model.RequirementId == 0)
                    {
                        <text>$("#discountRequirementContainer").trigger('nopnewdiscountruleadded', [data.NewRequirementId]);</text>
                    }
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert('@Html.Raw(HttpUtility.JavaScriptStringEncode(T("Admin.Promotions.Discounts.Requirements.FailedToSave").Text))');
                }  
            });
        });
    });
</script>
    
<table class="adminContent">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.Products):
        </td>
        <td class="adminData">
            <div>@Html.EditorFor(model => model.Products) <input type="button" id="saveHasOneProductrequirement@(Model.RequirementId)" class="k-button" value="@T("Admin.Common.Save")" /></div>
            <div id="pnl-save-requirement-result@(Model.RequirementId)" style="display:none;">@T("Admin.Promotions.Discounts.Requirements.Saved")</div>

            <span id="discountrequirement-products-check-progress@(Model.RequirementId)" style="display: none;" class="please-wait">@T("Common.Wait...")</span>
            <input type="submit" id="btnAddNewDiscountRequirementProduct@(Model.RequirementId)" value="@T("Plugins.DiscountRules.HasOneProduct.Fields.Products.AddNew")" onclick="javascript:OpenWindow('@(Url.RouteUrl("Plugin.DiscountRules.HasOneProduct.ProductAddPopup", new { btnId = "btnRefreshDiscountRequirementProducts" + Model.RequirementId, productIdsInput = Html.FieldIdFor(model => model.Products) }))', 800, 800, true); return false;" class="k-button" />
            <input type="submit" id="btnRefreshDiscountRequirementProducts@(Model.RequirementId)" style="display: none" />
            <script type="text/javascript">
                $(document).ready(function () {
                    $('#btnRefreshDiscountRequirementProducts@(Model.RequirementId)').click(function () {
                        //refresh product list
                        loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)();

                        //return false to don't reload a page
                        return false;
                    });
                });

                $(document).ready(function () {
                    loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)();
                });
                $(document).ready(function () {
                    $('#@Html.FieldIdFor(model => model.Products)')
                        .data('timeout', null)
                        .keyup(function() {
                            clearTimeout($(this).data('timeout'));
                            //one second timeout
                            $(this).data('timeout', setTimeout(loadDiscountRequirementProductFriendlyNames@(Model.RequirementId), 1000));
                    });
                });
                function loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)() {
                    var inputValue = $('#@Html.FieldIdFor(model => model.Products)').val();
                    if (inputValue) {
                        //load friendly names
                        $('#discountrequirement-products-check-progress@(Model.RequirementId)').show();
                            
                        var postData = {
                            productIds: inputValue
                        };
                        addAntiForgeryToken(postData);

                        $.ajax({
                            cache: false,
                            type: 'POST',
                            url: '@Url.RouteUrl("Plugin.DiscountRules.HasOneProduct.LoadProductFriendlyNames")',
                            data: postData,
                            dataType: 'json',
                            success: function(data) {
                                $('#discountrequirement-products-check-progress@(Model.RequirementId)').hide();
                                $('#discountrequirement-product-names@(Model.RequirementId)').text(data.Text);
                            },
                            failure: function () {
                                $('#discountrequirement-products-check-progress@(Model.RequirementId)').hide();
                                $('#discountrequirement-product-names@(Model.RequirementId)').text('');
                            }
                        });
                    } else {
                        $('#discountrequirement-product-names@(Model.RequirementId)').text('');
                    }
                }
            </script>
            <span id="discountrequirement-product-names@(Model.RequirementId)"></span>
        </td>
    </tr>
</table>